package com.yunang.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunang.oa.common.R;
import com.yunang.oa.entity.OaTask;
import com.yunang.oa.service.OaTaskService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yunang.oa.service.OaTaskPlusService;

import java.util.List;


@RestController
@RequestMapping("/v1/oa/task")
public class OaTaskController {

    private final OaTaskService oaTaskService;

    private final OaTaskPlusService oaTaskPlusService;

    @Autowired
    public OaTaskController(OaTaskService oaTaskService,OaTaskPlusService oaTaskPlusService) {
        this.oaTaskService = oaTaskService;
        this.oaTaskPlusService=oaTaskPlusService;
    }



    @ApiOperation(value = "增加任务")
    @PostMapping("/add")
    public R addOaTask(@RequestBody(required = true) OaTask task) {

//        boolean b = oaTaskPlusService.save(task);
//        return R.ok(b);
        Integer i = oaTaskService.insertOaTask(task);
        return R.ok(i>0 ? "success" :"failed");
    }

    @ApiOperation(value = "删除任务")
    @PostMapping("/delete/{id}")
    public R removeById(@PathVariable("id") Long id) {

        boolean remove = oaTaskPlusService.removeById(id);
        return R.ok(remove ? "success" : "error");
    }

    @ApiOperation(value = "修改任务")
    @PostMapping("/save")
    public R updateOaTask(@RequestBody(required = true) OaTask task) {

//        Long id= task.getTask_id();
//        OaTask oaTask = oaTaskPlusService.getById(id);
//        BeanUtils.copyProperties(task,oaTask);
//        boolean save = oaTaskPlusService.saveOrUpdate(OaTask);

        Long id = task.getTaskId();
        OaTask oaTask = oaTaskPlusService.getById(id);
        BeanUtils.copyProperties(task,oaTask);
        boolean save = oaTaskPlusService.saveOrUpdate(oaTask);
//        boolean b =oaTaskPlusService.updateById(task);
        return R.ok(save);
    }


    @ApiOperation(value = "查找所有任务")
    @GetMapping("/findAll")
    public R getOaTaskRList() {
        return R.ok(oaTaskService.getOaTaskList());
    }

    @ApiOperation(value = "通过任务id查找任务")
    @GetMapping("/getById/{id}")
    public R getOaTaskById(@PathVariable("id") Long id) {
        return R.ok(oaTaskService.getOaTaskById(id));
    }

    @ApiOperation(value = "通过任务名称查找任务")
    @GetMapping("/getByName/{name}")
    public R getOaTaskByName(@PathVariable("name") String name){

        QueryWrapper<OaTask> wrapper = new QueryWrapper<>();

        wrapper.like("task_name",name);

        List<OaTask> list = oaTaskPlusService.list(wrapper);

        return R.ok(list);
    }


    @ApiOperation(value = "通过任务接收人查找任务")
    @GetMapping("/getByTaskReceiver/{taskReceiver}")
    public R<Object> getOaTaskByReceiver(@PathVariable("taskReceiver") Long taskReceiver) {
        List<OaTask> task = oaTaskService.getOaTaskListByReceiver(taskReceiver);
        return R.ok(task);
    }

    @ApiOperation(value = "通过任务状态查找任务")
    @GetMapping("/getByStatus/{status}")
    public R<Object> getOaTaskByStatus(@PathVariable("status") Integer status) {
        List<OaTask> task = oaTaskService.getOaTaskListByStatus(status);
        return R.ok(task);
    }

}